    //
//  TransitionAnimationController.m
//  AnimationStudy
//
//  Created by longhuihu on 11-6-24.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TransitionAnimationController.h"

@interface TransitionViewBase : UIControl
{
	NSArray *imageArray;
	UIImageView* imageView;
	NSInteger imageIndex;
}
- (IBAction)actionStartAnimation:(id)sender;
- (void)startAnimation;
@end

@implementation TransitionViewBase

-(id)initWithFrame:(CGRect)frame
{
	if (self=[super initWithFrame:frame]) {
		[self addTarget:self action:@selector(actionStartAnimation:) forControlEvents:UIControlEventTouchUpInside];
		imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, self.bounds.size.width-10, self.bounds.size.height-10)];
		[self addSubview:imageView];
		[imageView release];
		
		imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"scene1.jpg"],
					  [UIImage imageNamed:@"scene2.jpg"],
					  [UIImage imageNamed:@"scene3.jpg"],
					  [UIImage imageNamed:@"scene4.jpg"],
					  [UIImage imageNamed:@"scene5.jpg"],nil];
		[imageArray retain];
		
		imageIndex = 0;
		[imageView setImage:[imageArray objectAtIndex:imageIndex]];
	}
	return self;
}

-(void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	const CGFloat color[] = {1,1,1,1.0};
	const CGFloat linieColor[] = {0,0.5,1,1.0};
	CGContextSetFillColor(context, color);
	CGContextSetStrokeColor(context, linieColor);
	CGContextSetLineWidth(context, 2);
	CGContextFillRect(context, self.bounds);
	CGContextStrokeRect(context, CGRectMake(1, 1, self.bounds.size.width-2, self.bounds.size.height-2));
}
- (IBAction)actionStartAnimation:(id)sender
{
	[self startAnimation];
}
- (void)startAnimation
{
}
@end

		 
@interface TRansitionFlip : TransitionViewBase
@end
	
@implementation TRansitionFlip
- (void)startAnimation
{
	imageIndex++;
	imageIndex = imageIndex%[imageArray count];
	[UIView transitionWithView:imageView
					  duration:1.0
					   options:UIViewAnimationOptionTransitionFlipFromLeft					
					animations:^(void){
						[imageView setImage:[imageArray objectAtIndex:imageIndex]];
					}
					completion:nil
	 ];	
}
@end

@interface TRansitionCurl : TransitionViewBase
@end

@implementation TRansitionCurl
- (void)startAnimation
{
	imageIndex++;
	imageIndex = imageIndex%[imageArray count];
	[UIView transitionWithView:imageView
					  duration:1.0
					   options:   UIViewAnimationOptionTransitionCurlUp					
					animations:^(void){
						[imageView setImage:[imageArray objectAtIndex:imageIndex]];
					}
					completion:nil
	 ];	
}
@end

@interface TRansitionToggle : TransitionViewBase
{
	BOOL showSecond;
	UIImageView *secondView;
}
@end
@implementation TRansitionToggle
-(id)initWithFrame:(CGRect)frame
{
	if (self=[super initWithFrame:frame]) {
		secondView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, self.bounds.size.width-10, self.bounds.size.height-10)];
		showSecond = NO;
		[imageView retain];
	}
	return self;
}
- (void)startAnimation
{
	imageIndex++;
	imageIndex = imageIndex%[imageArray count];
	showSecond = !showSecond;
	if (showSecond) {
		[secondView setImage:[imageArray objectAtIndex:imageIndex]];
	} else {
		[imageView setImage:[imageArray objectAtIndex:imageIndex]];
	}


	[UIView transitionFromView:showSecond?imageView:secondView
						toView:showSecond?secondView:imageView
					  duration:1.0
					   options:UIViewAnimationOptionTransitionCurlUp					
					completion:nil
	 ];	
}
@end


@implementation TransitionAnimationController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	TRansitionFlip *flip = [[TRansitionFlip alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
	[self.view addSubview:flip];
	[flip release];
	
	TRansitionCurl *curl = [[TRansitionCurl alloc] initWithFrame:CGRectMake(160, 0, 150, 150)];
	[self.view addSubview:curl];
	[curl release];
	
	TRansitionToggle *toggle = [[TRansitionToggle alloc] initWithFrame:CGRectMake(0, 160, 150, 150)];
	[self.view addSubview:toggle];
	[toggle release];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
